package com.scs.application.config;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.scs.application.core.mvc.converter.StringToDateConverter;
import com.scs.application.core.mvc.converter.StringTrimConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.List;

public class WebMvcConfig implements WebMvcConfigurer {
    @Value("${res.real-path}")
    private String resRealPath;

    @Autowired
    private FastJsonHttpMessageConverter fastJsonHttpMessageConverter;

    /**
     * 这个地方要重新注入一下资源文件，不然不会注入资源的，也没有注入requestHandlerMappping,相当于xml配置的
     * <p>
     * <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
     * <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
     * </p>
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        // 资源路径映射，用于存放运行时上传的文件
        registry.addResourceHandler("/res/**")
                .addResourceLocations("file:" + resRealPath);
    }


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {


        converters.add(fastJsonHttpMessageConverter);

        //处理字符串, 避免直接返回字符串的时候被添加了引号
        StringHttpMessageConverter smc = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converters.add(smc);

        SerializeConfig serializeConfig = SerializeConfig.globalInstance;
        serializeConfig.put(Long.class, ToStringSerializer.instance);
        serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
    }


//    @Bean
//    public AccessLogInterceptor accessLogInterceptor() {
//        return new AccessLogInterceptor();
//    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        InterceptorRegistration ir1 = registry.addInterceptor(accessLogInterceptor());
//        // 配置不拦截的路径
//        ir1.excludePathPatterns("/webjars/**", "/druid/**", "/swagger/**",
//                "/v2/api-docs", "/swagger-ui.html", "/swagger-resources/**","/ureport/**");
//
//        InterceptorRegistration ir2 = registry.addInterceptor(demoModeInterceptor());
//        // 配置不拦截的路径
//        ir2.excludePathPatterns("/webjars/**", "/druid/**", "/swagger/**",
//                "/v2/api-docs", "/swagger-ui.html", "/swagger-resources/**");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(false)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600);
    }


    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringTrimConverter());
        registry.addConverter(new StringToDateConverter());
    }
}
